package com.feifan.web.controller.utils;

import com.feifan.common.utils.StringUtils;
import com.feifan.oa.domain.FlowCategoryInfo;
import com.feifan.oa.domain.Leave;
import com.feifan.oa.domain.LeaveCheck;
import com.feifan.oa.service.ILeaveCheckService;
import com.feifan.system.domain.SysRole;
import com.feifan.system.domain.SysUser;
import com.feifan.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @program: 非凡
 * @description 请假审批工具类
 * @author: Alex
 * @create: 2020-07-11 01:35
 **/
@Component
public class ApprovedUtil {

    private static ApprovedUtil approvedUtil;
    @Autowired
    private ILeaveCheckService leaveCheckService;
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 获取下一个审批人id
     * @param userId
     */
    public static Long getNextId(Long deptId, Long userId) {
        //获取上级角色
        List<SysRole> sysRole = approvedUtil.sysRoleService.selectRolesByUserId2(userId);
        if (StringUtils.isEmpty(sysRole)) {
            System.out.println("没有该用户");
            return -2L;
        }
        if (StringUtils.isNull(sysRole.get(0).getSysRole())) {
            System.out.println("该用户没有上级角色");
            return -1L;
        }
        System.out.println("请假人AND审批人OfRole -> " + sysRole.get(0).getRoleName());
        List<SysRole> sysRoleList =
                approvedUtil.sysRoleService.selectUserRelationRole(sysRole.get(0).getSysRole().getRoleId());
        if (sysRoleList.size() == 0) {
            SysRole sysRole1 = approvedUtil.sysRoleService.selectRoleById(sysRole.get(0).getSysRole().getRoleId());
            sysRoleList = approvedUtil.sysRoleService.selectUserRelationRole(sysRole1.getSysRole().getRoleId());
        }
        //再进行一次部门过滤 过滤出同部门的
        for (SysRole role : sysRoleList) {
            //查询本部门内的上级
            if (role.getCurrSysUser().getDeptId().equals(deptId)) {
                //设置上级审批用户id
                userId = role.getCurrSysUser().getUserId();
                System.out.println("部门过滤" + role.getRoleName() + role.getCurrSysUser().getUserId());
                break;
            }
            //查询部门外的上级
            if (!role.getCurrSysUser().getDeptId().equals(deptId) && !userId.equals(role.getCurrSysUser().getUserId())) {
                //上级用户赋值 多层审批需要
                userId = role.getCurrSysUser().getUserId();
                System.out.println("部门过滤" + role.getRoleName() + role.getCurrSysUser().getUserId());
                break;
            }
        }

        return userId;
    }

    /**
     * 审批
     * @param i                需要几层审批
     * @param flowCategoryInfo 流程类别
     * @param deptId           部门id
     * @param userId           操作用户id
     */
    public static void approved(FlowCategoryInfo flowCategoryInfo, Leave leave, Integer i, Long deptId, long userId) {

        //当前审批人AND下一层审批人
        SysUser checkUser = new SysUser();
        SysUser nextUser = new SysUser();

        Long nextId = null;

        //获取下一层审批人
        userId = getNextId(deptId, userId);

        //审批
        for (int j = 0; j < i; j++) {
            //添加审批表
            LeaveCheck leaveCheck = new LeaveCheck();
            leaveCheck.setFlowCategoryInfo(flowCategoryInfo);
            leaveCheck.setLeave(leave);
            leaveCheck.setStatus(1);

            //设置当前审批和下层审批
            checkUser.setUserId(userId);
            leaveCheck.setCheckSysUser(checkUser);
            nextId = getNextId(deptId, userId);
            if (nextId == -2) {
                continue;
            }

            //如果当前审批和下一层审批id相同则把下一层审批id设置为0
            if (j + 1 == i || nextId == -1) {
                nextId = 0L;
            }
            //如果没有下级审批则重新开始下一次循环
            if (j != 0 && nextId.equals(nextUser.getUserId())) {
                continue;
            }
            nextUser.setUserId(nextId);
            leaveCheck.setNextSysUser(nextUser);

            //设置可见AND不可见
            if (j == 0) {
                leaveCheck.setIsShow(0);
            } else {
                leaveCheck.setIsShow(1);
            }

            approvedUtil.leaveCheckService.insertLeaveCheck(leaveCheck);

            userId = nextUser.getUserId();
        }
    }

    @PostConstruct
    public void init() {
        approvedUtil = this;
        approvedUtil.leaveCheckService = this.leaveCheckService;
        approvedUtil.sysRoleService = this.sysRoleService;
    }
}
